﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CGOnline.Server.Core;
using CGOnline.Server.Components;

namespace CGOnline.Server.UserLayer
{
    public class Room : RoomBase,IComponentProbe
    {
        List<UserBase> _userlist;
        List<Message> _msglist;

        //public Room()
        //{
        //    _userlist = new List<UserBase>();
        //    _msglist = new List<Message>();
        //}

        public Room(RoomConfig config)
        {
            _userlist = new List<UserBase>();
            _msglist = new List<Message>();

            if (config != null)
                Config = config;
        }

        internal override bool UserIn(UserBase user, object data)
        {
            if (user != null)
            {
                _userlist.Add(user);
                user.Disposing += user_Disposing;
                onBroadcast(this, RoomEventType.UserIn, user.Config.Copy());
                return true;
            }
            return false;
        }

        void user_Disposing(object sender, EventArgs e)
        {
            UserBase user = sender as UserBase;
            _userlist.Remove(sender as UserBase);
            onBroadcast(this, RoomEventType.UserLeave,user.Config.Copy() );
        }

        internal override bool UserLeave(UserBase user)
        {
            if (user != null)
            {
                user.Disposing -= user_Disposing;
                _userlist.Remove(user);
                onBroadcast(this, RoomEventType.UserLeave, user.Config.Copy());
                return true;
            }
            return false;
        }

        internal override bool UserSendMsg(UserBase user, Message msg)
        {
            if (user != null&&msg!=null)
            {
                //fill msg info
                msg.Sender = user.Config.UserIdentity;
                msg.SendTime = DateTime.Now;

                //msg in queue
                if(_msglist.Count>=Config.MaxMessageQueue)
                    _msglist.RemoveAt(0);
                _msglist.Add(msg);

                //broadcast
                onBroadcast(this, RoomEventType.NewMsg, msg);
                return true;
            }
            return false;
        }

        internal override List<Message> UserGetMsgs(UserBase user,DateTime time)
        {
            return _msglist.FindAll(ms=>
                (ms.Receiver==null||ms.Receiver==user.Config.UserIdentity)&&
                ms.SendTime>time)
                .ToList();
        }

        public override List<UserConfig> ListUser()
        {
            var usercfglist = new List<UserConfig>();
            foreach (var user in _userlist)
                usercfglist.Add(user.Config.Copy() as UserConfig);
            return usercfglist;
        }

        public override void GameStart()
        {
            throw new NotImplementedException();
        }

        #region IComponentProbe Members

        public IComponentInner Component
        {
            get;
            set;
        }

        #endregion
    }
}
